home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Tasks / PedApplication.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  7.0 KB  |  329 lines

  1. /*    =================
  2.  *    PedApplication.cc
  3.  *    =================
  4.  */
  5.  
  6. #include "PedestalDebugging.h"
  7.  
  8. #include <DiskInit.h>
  9. #include <Menus.h>
  10. #include <ToolUtils.h>
  11. #include <Windows.h>
  12.  
  13. #include "PedApplication.hh"
  14.  
  15. // Apple Event Assistant classes.
  16. #include "AEARegistry.h"
  17. #include "AEAHandlerGetData.hh"
  18. #include "AEAAccessorWildFromNull.hh"
  19. #include "AEAAccessorWildFromList.hh"
  20. #include "AEAAccessorModelFromWild.hh"
  21. #include "AEAModelName.hh"
  22.  
  23. // Pedestal classes
  24. #include "Ped1AppProcess.hh"
  25. #include "PedHandlerOpenApp.hh"
  26. #include "PedHandlerQuit.hh"
  27. #include "PedWindow.hh"
  28. #include "PedAgent.hh"
  29. #include "PedAgentAboutBox.hh"
  30. #include "PedChore.hh"
  31.  
  32. // Xtep classes
  33. #include "XAutoReleasePoolObj.hh"
  34.  
  35.  
  36. PedApplication::PedApplication()
  37. : PedTask(NULL), mProcess(Ped1AppProcess::Me()), mModel(*this)
  38. , mAboutCmd(*this), mNewCmd(*this), mOpenCmd(*this), mQuitCmd(*this)
  39. , mPreInitDone(false), mOpenAppReceived(false), mOpenDocReceived(false)
  40. , mQuitReceived(false)
  41. {
  42.     SetName("Pedestal");
  43. }
  44.  
  45. PedApplication::~PedApplication()
  46. {
  47.     
  48. }
  49.  
  50. void
  51. PedApplication::InstallAECallbacks(AEAModelRoot &inAppModel)
  52. {
  53.     new PedHandlerOpenApp(*this);
  54.     //new CAEAOpenDocHandler;
  55.     new PedHandlerQuit(*this);
  56.     
  57.     new AEAHandlerGetData;
  58.     
  59.     
  60.     // Accesses of anything from null get redispatched as from the application.
  61.     new AEAAccessorWildFromNull(inAppModel);
  62.     // Accesses of anything from a list get redispatched for each item.
  63.     new AEAAccessorWildFromList;
  64.     // Access a property from a model object.
  65.     new AEAAccessorModelFromWild(cProperty, typeModelToken);
  66. }
  67.  
  68. void
  69. PedApplication::InstallMenuCommands()
  70. {
  71.     mMenuBar.AppleMenu().InstallCommand(&mAboutCmd, 'abou');
  72.     mMenuBar.FileMenu().InstallCommand(&mNewCmd, 'new ');
  73.     mMenuBar.FileMenu().InstallCommand(&mOpenCmd, 'open');
  74.     mMenuBar.FileMenu().InstallCommand(&mCloseCmd, 'clos');
  75.     mMenuBar.FileMenu().InstallCommand(&mQuitCmd, 'quit');
  76.     //mMenuBar.EditMenu().InstallCommand(&mUndoCmd, 'undo');
  77.     mMenuBar.EditMenu().InstallCommand(&mCutCmd, 'cut ');
  78.     mMenuBar.EditMenu().InstallCommand(&mCopyCmd, 'copy');
  79.     mMenuBar.EditMenu().InstallCommand(&mPasteCmd, 'pste');
  80.     mMenuBar.EditMenu().InstallCommand(&mClearCmd, 'clea');
  81.     //mMenuBar.EditMenu().InstallCommand(&mSelectAllCmd, 'slct');
  82. }
  83.  
  84. void
  85. PedApplication::MyPreInit()
  86. {
  87.     InstallAECallbacks(mModel);
  88.     InstallMenuCommands();
  89. }
  90.  
  91. void
  92. PedApplication::PreInit()
  93. {
  94.     if (!mPreInitDone) {
  95.         MyPreInit();
  96.         mPreInitDone = true;
  97.     }
  98. }
  99.  
  100. void
  101. PedApplication::Initialize()
  102. {
  103.     PreInit();
  104. }
  105.  
  106. /*
  107.  *    --------------------------
  108.  *    Event processing routines.
  109.  *    --------------------------
  110.  */
  111.  
  112. void
  113. PedApplication::DispatchNullEvent(EventRecord &inEvent)
  114. {
  115.     WindowPtr window = ::FrontWindow();
  116.     WindowPeek peek = (WindowPeek)window;
  117.     if (peek && peek->windowKind == kPedestalWindowKind)
  118.         ((PedWindow *)GetWRefCon(window))->DispatchNullEvent(inEvent);
  119. }
  120.  
  121. void
  122. PedApplication::DispatchHighLevelEvent(EventRecord &inEvent)
  123. {
  124.     OSErr err = ::AEProcessAppleEvent(&inEvent);
  125. }
  126.  
  127. void
  128. PedApplication::DispatchMouseDown(EventRecord &inEvent)
  129. {
  130.     WindowPtr window;
  131.     Rect dragRect;
  132.     
  133.     if (inEvent.what != mouseDown)
  134.         return;
  135.  
  136.     switch (::FindWindow(inEvent.where, &window)) {
  137.         case inSysWindow:
  138.             ::SystemClick(&inEvent, window);
  139.             break;
  140.         case inMenuBar:
  141.             mMenuBar.ProcessMenuItem(::MenuSelect(inEvent.where));
  142.             break;
  143.         case inDrag:
  144.             dragRect = qd.screenBits.bounds;
  145.             ::DragWindow(window, inEvent.where, &dragRect);
  146.             break;
  147.         case inContent:
  148.             //::GlobalToLocal(&inEvent.where);
  149.             //WindowClick(window, inEvent.where);
  150.             if (((WindowPeek)window)->windowKind == kPedestalWindowKind)
  151.                 ((PedWindow *)GetWRefCon(window))->DispatchClickEvent(inEvent);
  152.             break;
  153.         case inGrow:
  154.             Rect sizeRect = {30, 50, 10000, 10000};
  155.             long dims = ::GrowWindow(window, inEvent.where, &sizeRect);
  156.             if (dims)
  157.                 if (((WindowPeek)window)->windowKind == kPedestalWindowKind)
  158.                     ((PedWindow *)GetWRefCon(window))->Resize(LoWord(dims), HiWord(dims));
  159.                 else
  160.                     ::SizeWindow(window, LoWord(dims), HiWord(dims), true);
  161.             break;
  162.         case inGoAway:
  163.             if (((WindowPeek)window)->windowKind == kPedestalWindowKind)
  164.                 ((PedWindow *)GetWRefCon(window))->ProcessGoAway(inEvent);
  165.             break;
  166.         default:
  167.             break;
  168.     }
  169. }
  170.  
  171. void
  172. PedApplication::DispatchKey(EventRecord &inEvent)
  173. {
  174.     if (inEvent.modifiers & cmdKey) {
  175.         if (inEvent.what == keyDown) // no commands on autoKey
  176.             mMenuBar.ProcessMenuItem(::MenuKey(inEvent.message & charCodeMask));
  177.     } else {
  178.         WindowPtr window = ::FrontWindow();
  179.         WindowPeek peek = (WindowPeek)window;
  180.         if (peek != NULL && peek->windowKind == kPedestalWindowKind)
  181.             ((PedWindow *)GetWRefCon(window))->DispatchKey(inEvent);
  182.     }
  183. }
  184.  
  185. void
  186. PedApplication::DispatchActivate(EventRecord &inEvent)
  187. {
  188.     if (inEvent.what != activateEvt)
  189.         return;
  190.     
  191.     if (((WindowPeek)inEvent.message)->windowKind == kPedestalWindowKind) {
  192.         PedWindow *wind = (PedWindow *)GetWRefCon(WindowPtr(inEvent.message));
  193.         if (inEvent.modifiers & activeFlag) {
  194.             wind->Activate();
  195.         } else {
  196.             wind->Deactivate();
  197.         }
  198.     }
  199.     
  200. }
  201.  
  202. void
  203. PedApplication::DispatchUpdate(EventRecord &inEvent)
  204. {
  205.     if (inEvent.what != updateEvt)
  206.         return;
  207.     
  208.     //::BeginUpdate(WindowPtr(inEvent.message));
  209.     if (((WindowPeek)inEvent.message)->windowKind == kPedestalWindowKind)
  210.         ((PedWindow *)GetWRefCon(WindowPtr(inEvent.message)))->Update();
  211.     //::EndUpdate(WindowPtr(inEvent.message));
  212. }
  213. void
  214. PedApplication::DispatchDiskInsert(EventRecord &inEvent)
  215. {
  216.     if (inEvent.what != updateEvt)
  217.         return;
  218.     if (HiWord(inEvent.message) == 0)
  219.         return;
  220.     
  221.     Point pt = {100, 100};
  222.     
  223.     ::DILoad();
  224.     ::DIBadMount(pt, inEvent.message);
  225.     ::DIUnload();
  226. }
  227.  
  228.  
  229. void
  230. PedApplication::DispatchEvent(EventRecord &inEvent)
  231. {
  232.     switch (inEvent.what) {
  233.         case nullEvent:
  234.             DispatchNullEvent(inEvent);
  235.             break;
  236.         case kHighLevelEvent:
  237.             DispatchHighLevelEvent(inEvent);
  238.             break;
  239.         case mouseDown:
  240.             DispatchMouseDown(inEvent);
  241.             break;
  242.         case keyDown:
  243.         case autoKey:
  244.             DispatchKey(inEvent);
  245.             break;
  246.         case activateEvt:
  247.             DispatchActivate(inEvent);
  248.             break;
  249.         case updateEvt:
  250.             DispatchUpdate(inEvent);
  251.             break;
  252.         case diskEvt:
  253.             DispatchDiskInsert(inEvent);
  254.             break;
  255.         default:
  256.             break;
  257.     }
  258. }
  259.  
  260. void
  261. PedApplication::EventLoop()
  262. {
  263.     EventRecord evt;
  264.     long sleepTicks = 10;
  265.     RgnHandle mouseRgn = NULL;
  266.     
  267.     XAutoReleasePoolObj pool;
  268.     
  269.     // Use two levels of looping.
  270.     // This lets us loop inside the try block without entering and leaving,
  271.     // and will continue looping if an exception is thrown.
  272.     while (!mQuitReceived) {
  273.         try {
  274.             while (!mQuitReceived) {
  275.                 pool.Flush();
  276.                 DoRepeatChores();
  277.                 if (::WaitNextEvent(everyEvent, &evt, sleepTicks, mouseRgn)) {
  278.                     DispatchEvent(evt);
  279.                 } else {
  280.                     DispatchNullEvent(evt);
  281.                     DoIdleChores();
  282.                 }
  283.                 if (mOpenDocReceived && !mOpenAppReceived) {
  284.                     mQuitReceived = true;
  285.                 }
  286.             }
  287.         } catch (...) {
  288.             DebugBeep();
  289.         }
  290.     }
  291. }
  292.  
  293. void
  294. PedApplication::Run()
  295. {
  296.     Initialize();
  297.     EventLoop();
  298. }
  299.  
  300. void
  301. PedApplication::NotifyOpenAppEvent()
  302. {
  303.     mOpenAppReceived = true;
  304. }
  305.  
  306. void
  307. PedApplication::NotifyOpenDocEvent()
  308. {
  309.     mOpenDocReceived = true;
  310. }
  311.  
  312. void
  313. PedApplication::NotifyPrintDocEvent()
  314. {
  315.     mOpenDocReceived = true;
  316. }
  317.  
  318. void
  319. PedApplication::NotifyQuitEvent()
  320. {
  321.     mQuitReceived = true;
  322. }
  323.  
  324. void
  325. PedApplication::DoAbout()
  326. {
  327.     mAboutBox.OpenWindow();
  328. }
  329.